package com.chencong.service;

import com.alibaba.fastjson.JSONObject;
import com.chencong.entity.Provider;
import com.chencong.request.Request;
import com.chencong.util.Constant;
import java.io.DataOutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chencong
 * @date 2020/12/15 上午10:22
 */
public class ProviderService {
    private DataOutputStream dos;
    private Request request;
    private int maxID = -1;
    private PageService ps;//用来弹出页面信息
    private FileService fs;//用来向文件读写数据

    public ProviderService() {
        this.fs = new FileService();
    }

    public ProviderService(DataOutputStream dos, Request request) {
        this.dos = dos;
        this.request = request;
        this.ps = new PageService(dos);
        this.fs = new FileService();
    }

    /**
     * 查询服务商列表
     */
    public void getProvidersList() throws Exception {
        String data = request.getData();
        Provider provider = JSONObject.parseObject(data, Provider.class);
//        System.out.println(provider);
        List<Provider> providers = loadProvider();
        //name和desc最少一个有值
        if (provider!=null&&provider.getName().length() + provider.getDesc().length() != 0) {
            List<Provider> providers1 = providers.stream().filter(provider1 -> provider1.getName().contains(provider.getName()) && provider1.getDesc().contains(provider.getDesc())).collect(Collectors.toList());
            providers = providers1;
        }
        //name和desc都没有值,查询的是所有
        ps.sendData(providers);
    }

    /**
     * 加载供应商数据库
     *
     * @return
     * @throws Exception
     */
    public List<Provider> loadProvider() throws Exception {
        String json = fs.read(Constant.SRC_PROVIDERS);
        List<Provider> providers = JSONObject.parseArray(json, Provider.class);
        if (maxID == -1) {
            maxID = providers.stream().max((o1, o2) -> o1.getId() - o2.getId()).get().getId();
        }
        return providers;
    }

    /**
     * 添加供应商
     *
     * @param data
     * @return
     * @throws Exception
     */
    public boolean addProvider(String data) throws Exception {
        Provider provider = JSONObject.parseObject(data, Provider.class);
        System.out.println(provider);
        //校验供应商是否合法
        String s = checkProvider(provider);
        if (!s.equals("true")) {
            ps.sendData(s, false);
            return false;
        }
        List<Provider> providers = loadProvider();
        long count = providers.stream().filter(provider1 -> provider1.getName().equals(provider.getName())).count();
        if (count != 0) {
            System.out.println("供应商已存在");
            ps.sendData("供应商已存在", false);
            return false;
        }

        provider.setId(++maxID);
        providers.add(provider);
        fs.write(Constant.SRC_PROVIDERS, providers);
        ps.sendData("操作成功");
        return true;
    }

    /**
     * 校验供应商是否合法
     *
     * @param provider
     * @return
     */
    public String checkProvider(Provider provider) {
//        System.out.println("调用了检查供应商方法");
        if (provider.getName().length() == 0) {
            System.out.println("名称不能为空");
            return "名称不能为空";
        } else if (provider.getDesc().length() == 0) {
            System.out.println("供应商描述不能为空");
            return "供应商描述不能为空";
        } else if (provider.getPhone().length() == 0) {
            System.out.println("供应商电话不能为空");
            return "供应商电话不能为空";
        } else if (provider.getContactPerson().length() == 0) {
            System.out.println("联系人不能为空");
            return "联系人不能为空";
        }
        return "true";
    }

    /**
     * 供应商信息查询
     *
     * @param data
     */
    public void getProvider(String data) throws Exception {
        Provider provider = JSONObject.parseObject(data, Provider.class);
        List<Provider> providers = loadProvider();
        List<Provider> collect = providers.stream().filter(provider1 -> provider1.getId() == provider.getId()).collect(Collectors.toList());
        ps.sendData(collect.get(0));
    }

    /**
     * 供应商信息修改
     *
     * @param data
     */
    public void updateProvider(String data) throws Exception {
        Provider provider = JSONObject.parseObject(data, Provider.class);
        //校验用户是否合法
        String s = checkProvider(provider);
        if (!s.equals("true")) {
            ps.sendData(s, false);
            return;
        }
        List<Provider> providers = loadProvider();
        for (int i = 0; i < providers.size(); i++) {
            if (providers.get(i).getId() == provider.getId()) {
                providers.set(i, provider);
                break;
            }
        }
        System.out.println(providers);
        fs.write(Constant.SRC_PROVIDERS, providers);
        ps.sendData("操作成功");
    }

    /**
     * 删除供应商
     *
     * @param data
     */
    public void deleteProvider(String data) throws Exception {
        Provider provider = JSONObject.parseObject(data, Provider.class);
        List<Provider> providers = loadProvider();
        Provider provider2 = providers.stream().filter(provider1 -> provider1.getId() == provider.getId()).findFirst().get();
        providers.remove(provider2);
        fs.write(Constant.SRC_PROVIDERS, providers);
        ps.sendData("操作成功");
    }
}

